from brian import *
from brian.library.random_processes import *
from brian.library.synapses import  *
import mydataINRG as mydatinrg
import Functions as func

def i_inj(t):
    if t <= 1.1*ms and t > 1 *ms:
        return 1.345 *nA
    else:
        return 0*nA

def current_inj(t):
    if t > 0*msecond:
        return -0.5*namp
    else:
        return 0*mamp
class NeuronFactory:
    
    def generate(self, size):
        mdIN = mydatinrg.INRGVars()
        print mdIN.area
        eqsIN=Equations('''
        dvm/dt = (I-(mdIN.gna*mna*mna*mna*hna*(vm-mdIN.Ena))-(mdIN.gk*mk*mk*mk*mk*(vm-mdIN.Ek))-mdIN.gl*(vm-mdIN.El)+ge_current)/mdIN.cpf : mvolt
        dmna/dt = (func.m_inf_na(vm/mvolt)-mna)/mdIN.taumna : 1
        dhna/dt = (func.h_inf_na(vm/mvolt)-hna)/func.tau_h_na(vm/mvolt) : 1
        dmk/dt = (func.m_inf_k(vm/mvolt)-mk)/func.tau_m_k(vm/mvolt) : 1
        ''')
        eqsIN+=alpha_conductance(input='ge',E=mdIN.Ee, tau=mdIN.taue,conductance_name='epsp') 
        eqsIN+=OrnsteinUhlenbeck('I',mu=0*nA,sigma=0.002*nA,tau=10*ms)
        IN=NeuronGroup(size,model=eqsIN,
        threshold=EmpiricalThreshold(threshold=-20*mV), 
        implicit=True)
        
        return IN
     
    def initNet(self, IN, init='def'): 
        mdIN = mydatinrg.INRGVars()
        vin =mdIN.vinit
        if init != 'def':
            vin=init
        IN.vm=vin*mV
        IN.mna = func.m_inf_na(vin)
        IN.hna = func.h_inf_na(vin)
        IN.mk = func.m_inf_k(vin);
        #IN.El = mdIN.El*mV 
        return IN